<template>
	<view class="data-container">
		<!-- 业绩 -->
		<view class="data-box">
			<view class="data-title">
				<view class="data-title-text">
					<view>业绩目标</view>
					<view style="width: 260rpx; margin-left: 20rpx;">
						<u-subsection mode="subsection" @change="change" :list="list" :current="current"></u-subsection>
					</view>
				</view>
				<view class="data-title-more" @click="goDetails">
					<text>详情</text>
					<uni-icons type="right" size="18" color="#000000"></uni-icons>
				</view>
			</view>
			<!-- 环图 -->
			<view class="ring-box">
				<view style="width: 400rpx; height: 400rpx;">
					<l-echart ref="chart"></l-echart>
				</view>
				<view class="money-box">
					<view>
						<view class="money-title">目标金额</view>
						<view class="money-num">￥{{ chartData.annualTarget }}</view>
					</view>
					<view>
						<view class="money-title">完成金额</view>
						<view class="money-num">￥{{ chartData.totalAmount }}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 成交排名 -->
		<view class="data-box">
			<view class="data-title">
				<view class="data-title-text">
					<view>成交排名</view>
					<view style="width: 260rpx; margin-left: 20rpx;">
						<u-subsection mode="subsection" @change="changeRank" :list="list" :current="currentRank"></u-subsection>
					</view>
				</view>
				<view class="data-title-more" @click="goDeal()">
					<text>详情</text>
					<uni-icons type="right" size="18" color="#000000"></uni-icons>
				</view>
			</view>
			<view class="rank-box">
				<view class="rank-head">
					<view class="left-box">排名</view>
					<view class="right-box">
						<view class="name-box">姓名</view>
						<view class="money-box">成交额</view>
					</view>
				</view>
				<view class="rank-list">
					<template v-if="rankList.length">
						<view class="rank-item" v-for="(item, index) in rankList" :key="index">
							<view class="left-box">{{ item.ranking }}</view>
							<view class="right-box">
								<view class="top-box">
									<view class="name-box">{{ item.userName }}</view>
									<view class="money-box">￥{{ item.totalAmount }}</view>
								</view>
								<!-- <view class="bottom-box">
									<u-line-progress :percentage="item.conversionRate" :showText="false"></u-line-progress>
								</view> -->
							</view>
						</view>
						<view class="rank-item">
							<view class="left-box">合计:</view>
							<view class="right-box">
								<view class="top-box">
									<view>￥{{ totalRankAmount }}</view>
								</view>
							</view>
						</view>
					</template>
					<empty-view v-else/>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as echarts from '@/uni_modules/lime-echart/static/echarts.min'
	import { calculateDateRange } from '@/common/utils'
	export default {
		data() {
			return {
				rankList: [],
				chartData: {
					annualTarget: 0,
					totalAmount: 0,
					conversionRate: 0 // 完成比率
				},
				queryData: {
					pageNum: 1,
					pageSize: 10,
					performanceType: 2,
					startTime: null,
					endTime: null
				},
				list: ['本日', '本月', '本年'],
				current: 2,
				currentRank: 2
			}
		},
		created() {
			const [start, end] = calculateDateRange(1)
			this.queryData.startTime = start
			this.queryData.endTime = end

			this.getData()
			// 个人 成交排名
			this.getPersonage()
			setTimeout(() => {
				this.InfinityChart()
			}, 500)
		},
		computed: {
			totalRankAmount() {
				let total = this.rankList.reduce((acc, item) => acc + Number(item.totalAmount), 0)
				return total.toFixed(2)
			}
		},
		methods: {
			change(index) {
				this.current = index;
				this.getData()
			},
			changeRank(index) {
				this.currentRank = index;
				if(index == 0) {
					const [start, end] = calculateDateRange(9)
					this.queryData.startTime = start
					this.queryData.endTime = end
				} else if(index == 1) {
					const [start, end] = calculateDateRange(4)
					this.queryData.startTime = start
					this.queryData.endTime = end
				} else if(index == 2) {
					const [start, end] = calculateDateRange(1)
					this.queryData.startTime = start
					this.queryData.endTime = end
				}


				this.getPersonage()
			},
			getData() {
				let keyword = this.current == 0? '1009' : this.current == 1? '1005'  : this.current == 2? '1001' : ''
				this.$http.get('/app/statistics/performance/yearList', { keyword: keyword }).then(res => {
					// console.log(res)
					if (res.code == 200) {
						this.chartData.annualTarget = res.data.annualTarget
						this.chartData.totalAmount = res.data.totalAmount
						this.chartData.conversionRate = res.data.conversionRate
						console.log(this.chartData)
					} else {
						uni.$u.toast(res.msg)
					}
				})
			},
			getPersonage() {
				this.$http.get('/app/statistics/performance/personageOrderList', this.queryData).then(res => {
					console.log(res)
					if (res.code == 200) {
						this.rankList = res.rows
					}
				})
			},
			// 业绩详情
			goDetails() {
				uni.navigateTo({
					url: '/pages/index/performance'
				})
			},
			// 成交数据排名
			goDeal() {
				uni.navigateTo({
					url: '/pages/crm/report/deal'
				})
			},
			// },
			InfinityChart() {
				// console.log('初始化环图')
				this.$refs.chart.init(echarts, chart => {
					let option = {
						tooltip: {
							trigger: 'item',
							show: false
						},
						legend: {
							show: false,
							top: '5%',
							left: 'center'
						},
						series: [{
							name: '访问来源',
							type: 'pie',
							radius: ['40%', '70%'],
							avoidLabelOverlap: false,
							label: {
								show: false,
								position: 'center'
							},
							emphasis: {
								label: {
									show: true,
									fontSize: 13,
									fontWeight: 'bold'
								}
							},
							labelLine: {
								show: false
							},
							data: [
								{ value: this.chartData.annualTarget, name: '目标金额' },
								{ value: this.chartData.totalAmount, name: '完成金额' }
							]
						}]
					}
					chart.setOption(option);

				});
			},
			initChartColumnar() {
				this.$refs.chartColumnar.init(echarts, chart => {
					const option = {
						tooltip: {
							trigger: 'axis',
							axisPointer: {
								type: 'shadow'
							}
						},
						legend: {
							data: ['2011年', '2012年']
						},
						grid: {
							left: '3%',
							right: '4%',
							bottom: '3%',
							containLabel: true
						},
						xAxis: {
							type: 'value',
							boundaryGap: [0, 0.01]
						},
						yAxis: {
							type: 'category',
							data: ['巴西', '印尼', '美国', '印度', '中国', '世界人口(万)']
						},
						series: [{
								name: '2011年',
								type: 'bar',
								data: [18203, 23489, 29034, 104970, 131744, 630230]
							},
							{
								name: '2012年',
								type: 'bar',
								data: [19325, 23438, 31000, 121594, 134141, 681807]
							}
						]
					}
					chart.setOption(option);

				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	.data-box {
		background: white;
		padding: 30rpx 30rpx;

		.data-title {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.data-title-text {
				font-size: 32rpx;
				font-weight: 500;
				color: rgba(12, 16, 25, 1);
				display: flex; align-items: center;
			}

			.data-title-more {
				display: flex;
				align-items: center;

				text {
					font-size: 28rpx;
					color: rgba(103, 117, 133, 1);
				}
			}
		}

		.ring-box {
			display: flex;

			.money-box {
				display: flex;
				flex-direction: column;
				justify-content: space-around;

				.money-title {
					font-size: 24rpx;
					color: rgba(103, 117, 133, 1);
					margin-bottom: 16rpx;
				}

				.money-num {
					font-size: 48rpx;
					font-weight: 500;
					color: rgba(0, 0, 0, 1);
				}
			}
		}
		.rank-box {
			.left-box {
				width: 100rpx;
			}
			.name-box {
				flex: 1;
			}
			.money-box {
				width: 160rpx;
			}
		}
		.rank-head {
			margin: 40rpx 0;
			display: flex;
			font-size: 24rpx;
			font-weight: 400;
			color: rgba(103, 117, 133, 1);
			.right-box {
				flex: 1;
				display: flex;
			}
		}
		.rank-item {
			display: flex;
			align-items: center;
			margin-bottom: 40rpx;
			.right-box {
				flex: 1;
				.top-box {
					display: flex;
					font-size: 28rpx;
					font-weight: 500;
					color: rgba(12, 16, 25, 1);
					// margin-bottom: 10rpx;

				}
			}
		}
	}
</style>